package mysql

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Account  string `gorm:"type:varchar(20);not null;comment:'账号'"`
	Password string `gorm:"type:char(32);not null;comment:'密码'"`
	Mobile   string `gorm:"type:char(11);not null;comment:'手机号'"`
}

type Shop struct {
	gorm.Model
	UserId int     `gorm:"type:int;not null;comment:'用户id'"`
	Name   string  `gorm:"type:varchar(20);not null;comment:'商品名称'"`
	Price  float64 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	Stock  int     `gorm:"type:int;not null;comment:'商品库存'"`
	Status int     `gorm:"enum('上架','下架','审核失败','审核成功');not null;comment:'商品状态'"`
}

type ShopClass struct {
	gorm.Model
	UserId int    `gorm:"type:int;not null;comment:'用户id'"`
	ShopId int    `gorm:"type:int;not null;comment:'商品id'"`
	Name   string `gorm:"type:varchar(20);not null;comment:'商品分类名称'"`
}

type Status struct {
	gorm.Model
	ShopId int `gorm:"type:int;not null;comment:'商品id'"`
	Status int `gorm:"enum('上架','下架','审核失败','审核成功');not null;comment:'商品状态'"`
}

func (g *Shop) Get(db *gorm.DB) error {
	// 再唠叨一下，事务一旦开始，你就应该使用 tx 处理数据
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Create(&g).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
